package com.rtp2p.rtcoresdkdemo.add.ap

import android.app.Application
import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.rtp2p.rtcoresdkdemo.base.BaseViewModel
import com.rtp2p.rtp2pcore.core.BindingApDevice
import com.rtp2p.rtp2pcore.core.BindingDevice
import com.rtp2p.rtp2pcore.core.SearchApDevice

class AddBindingApViewModel(application: Application) : BaseViewModel(application) {
    val msg = MutableLiveData<String>()
    val isShowToWiFiBtn = MutableLiveData<Boolean>()
    var adapter: AddBindingApAdapter? = null
        private set

    private var isBinding = false
    private var searchApDevice: SearchApDevice? = null

    //绑定设备
    private val bindingDevice = BindingDevice()
    fun init(context: Context, ssid: String, pwd: String, finishCallback: () -> Unit) {
        adapter = AddBindingApAdapter { deviceId ->
            startBindingApDevice(context, deviceId, ssid, pwd, finishCallback)
        }

        searchApDevice = SearchApDevice(context) {
            stopSearch()
            isShowToWiFiBtn.postValue(!it.success)
            if (it.success) {
                if ((adapter?.size() ?: 0) > 0) return@SearchApDevice
                adapter?.add(it.data)
            }
        }
    }

    internal fun onStart() {
        startSearch()
    }

    internal fun onStop() {
        stopSearch()
        stopBindingApDevice()
    }

    private fun startSearch() {
        isShowToWiFiBtn.value = false

        msg.value = "开始搜索"
        adapter?.clear()
        searchApDevice?.startSearch()
    }

    private fun stopSearch() {
        msg.value = ""
        searchApDevice?.stopSearch()
    }


    //配置网络
    private var bindingApDevice: BindingApDevice? = null
    private fun startBindingApDevice(
        context: Context,
        deviceId: String,
        ssid: String,
        pwd: String,
        callback: () -> Unit
    ) {
        if (bindingApDevice != null) return

        if (isBinding) return
        isBinding = true
        msg.value = "开始配网"

        bindingApDevice = BindingApDevice(context,deviceId)
        bindingApDevice?.configData(ssid, pwd) {
            stopBindingApDevice()
            if (it.success) {
                binding(context, it.data, callback)
            } else {
                msg.value = "配网失败： code:${it.code} message:${it.message}"
            }
        }
    }

    private fun stopBindingApDevice() {
        bindingApDevice?.destroy()
        bindingApDevice = null
    }

    private fun binding(
        context: Context,
        deviceId: String,
        finishCallback: () -> Unit
    ) {
        msg.value = "开始绑定"
        bindingDevice.binding(context, deviceId, "camera") {
            isBinding = false
            if (it.success) {
                msg.value = "$deviceId 绑定成功"
                Toast.makeText(context, "$deviceId 绑定成功", Toast.LENGTH_SHORT).show()
                //finishCallback()
            } else {
                msg.value = "绑定失败： code:${it.code} message:${it.message}"
            }
        }
    }

    fun toWiFiSet(context: Context) {
        context.startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
    }
}